package com.ztgf.commodity.api.cms;

import com.ztgf.commodity.models.bo.CategoryShieldBrandRelationBO;
import com.ztgf.commodity.models.bo.CategoryShieldBrandRelationSaveBO;
import com.ztgf.commodity.models.dto.CategoryShieldBrandRelationDTO;
import com.ztgf.commodity.models.service.ICategoryShieldBrandRelationService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 分类盾构品牌关系表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-02 15:11:41
 */

@RestController
@RequestMapping("/cms/categoryShieldBrandRelation")
@Slf4j
@Api(value = "CategoryShieldBrandRelationController", tags = "分类盾构品牌关系表接口")
public class CategoryShieldBrandRelationController {

    @DubboReference
    private ICategoryShieldBrandRelationService categoryShieldBrandRelationService;

    @PostMapping(value = "/insert")
    @ApiOperation(value = "添加分类盾构品牌关系表", notes = "添加分类盾构品牌关系表")
    public Result insert(CategoryShieldBrandRelationSaveBO saveBO) {
        boolean flag = categoryShieldBrandRelationService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存分类盾构品牌关系表", notes = "保存分类盾构品牌关系表")
    public Result save(CategoryShieldBrandRelationSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改分类盾构品牌关系表", notes = "修改分类盾构品牌关系表")
    public Result update(CategoryShieldBrandRelationSaveBO saveBO) {
        CategoryShieldBrandRelationDTO dto = new CategoryShieldBrandRelationDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = categoryShieldBrandRelationService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除分类盾构品牌关系表", notes = "删除分类盾构品牌关系表")
    @ApiImplicitParam(name = "id", value = "分类盾构品牌关系表id", required = true, paramType = "query", dataType = "Long")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = categoryShieldBrandRelationService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除分类盾构品牌关系表", notes = "批量删除分类盾构品牌关系表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        categoryShieldBrandRelationService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CategoryShieldBrandRelationDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        CategoryShieldBrandRelationDTO dto = categoryShieldBrandRelationService.selectById(id);
        return new Result<>(dto);
    }

    @PostMapping("/queryShieldBrandRelationshipList")
    @ApiOperation(value = "类目盾构品牌关系列表", notes = "类目盾构品牌关系列表")
    public Result queryShieldBrandRelationshipList(CategoryShieldBrandRelationBO bo) {
        return new Result<>(categoryShieldBrandRelationService.queryShieldBrandRelationshipList(bo));
    }
}
